package com.laycoding.order.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.laycoding.common.entity.Order;
import com.laycoding.order.mapper.OrderMapper;
import com.laycoding.order.service.OrderService;
import com.laycoding.order.service.feigin.GoodsService;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * created with IntelliJ IDEA.
 *
 * @author: guidada
 * @date: 2021/06/04/10:39
 * @description:
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CuratorFramework client;

    @Override
    public void createOrder(Integer userId, Integer goodId) {
     /*   InterProcessMutex lock = new InterProcessMutex(client, "/order");
        try {
            if (lock.acquire(30, TimeUnit.SECONDS)) {
                try {
                    Boolean hasKey = redisTemplate.hasKey("good:" + goodId);
                    if (hasKey) {
                        Map<String, Object> goodMap = redisTemplate.opsForHash().entries("good:" + goodId);
                        Integer stack = (Integer) goodMap.get("stack");
                        JSONObject goodInfo = JSONObject.parseObject(goodMap.get("info").toString());
                        if (stack > 0) {
                            redisTemplate.opsForHash().increment("good:" + goodId, "stack", -1);
                            Order order = new Order();
                            order.setGoodsId(goodId);
                            order.setUserId(userId);
                            order.setCreateDate(new Date());
                            order.setAddrId(10086);
                            order.setPayDate(new Date());
                            order.setGoodsName(goodInfo.get("goodsName").toString());
                            order.setGoodsPrice(Double.parseDouble(goodInfo.get("goodsPrice").toString()));
                            order.setGoodsCount(stack - 1);
                            this.baseMapper.insert(order);
                        }
                    }
                } finally {
                    lock.release();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }*/

        Boolean hasKey = redisTemplate.hasKey("good:" + goodId);
        if (hasKey) {
            synchronized (this) {
                Map<String, Object> goodMap = redisTemplate.opsForHash().entries("good:" + goodId);
                Integer stack = (Integer) goodMap.get("stack");
                JSONObject goodInfo = JSONObject.parseObject(goodMap.get("info").toString());
                if (stack > 0) {
                    redisTemplate.opsForHash().increment("good:" + goodId, "stack", -1);
                    Order order = new Order();
                    order.setGoodsId(goodId);
                    order.setUserId(userId);
                    order.setCreateDate(new Date());
                    order.setAddrId(10086);
                    order.setPayDate(new Date());
                    order.setGoodsName(goodInfo.get("goodsName").toString());
                    order.setGoodsPrice(Double.parseDouble(goodInfo.get("goodsPrice").toString()));
                    order.setGoodsCount(stack - 1);
                    this.baseMapper.insert(order);
                }
            }
        }

    }
}
